package com.yedean.mall.portal.client;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 抽象一层 把restTemplate的方法提取
 */
@AllArgsConstructor
public abstract class AbstractWxClient implements WxClient {
    
    protected final RestTemplate restTemplate;

    protected <T> T post(String url, MultiValueMap<String, Object> params, Class<T> res) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> post = new HttpEntity<>(params, headers);
        ResponseEntity<T> response = restTemplate.postForEntity(url, post, res);
        return response.getBody();
    }


}
